<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script type="text/javascript">
        window.onload = function (ev) {
            var currentDateDiv = document.getElementById("current_date");
            var strDateDiv = document.getElementById("str_date");
            var timestampDateDiv = document.getElementById("timestamp_date");

            var currentDate = new Date();
            var textNode = document.createTextNode(formatDate(currentDate));
            currentDateDiv.appendChild(textNode);

            var strDate = new Date("2012-02-02 10:10:10");
            textNode = document.createTextNode(formatDate(strDate));
            strDateDiv.appendChild(textNode);

            var timestampDate = new Date(936839349000);
            textNode = document.createTextNode(formatDate(timestampDate));
            timestampDateDiv.appendChild(textNode);

            function toTwoDigits(x) {
                return x < 10 ? "0" + x : "" + x;
            }

            /**
             * 格式化日期，注意month是需要加一的
             * @param date Date对象
             * @returns {string} 格式化之后的字符串
             */
            function formatDate(date) {
                var year = date.getFullYear();
                var month = toTwoDigits(date.getMonth() + 1);
                var day = toTwoDigits(date.getDate());
                var hour = toTwoDigits(date.getHours());
                var minutes = toTwoDigits(date.getMinutes());
                var seconds = toTwoDigits(date.getSeconds());
                return "" + year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds;
            }
        }
    </script>
</head>
<body>
当前时间：
<div id="current_date"></div>
<br/>
用字符串指定的时间：
<div id="str_date"></div>
<br/>
用时间戳指定的时间：
<div id="timestamp_date"></div>
<br/>

</body>
</html>
